package drrr.controller;

import drrr.orm.UserEntity;
import org.apache.struts2.ServletActionContext;

/**
 * Operate the Login action
 * 登陆
 * Created by Xun on 2015/4/14.
 */
public class LoginAction extends BaseAction {

    private String username;
    private String password;

    private UserEntity userEntity;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public String login() {
        userEntity = getAllService().getUserService().isLegal(username, password);
        if (userEntity != null) {
            ServletActionContext.getContext().getSession().put("user", userEntity);
            return this.SUCCESS;
        } else {
            this.addFieldError("username", "用户名或密码错误!");
            return this.INPUT;
        }
    }

    @Override
    public void validate() {
        if (this.getUsername().equals("")) {
            this.addFieldError("username", "用户名不能为空 ");
            return;
        }
        if (this.getPassword().equals("")) {
            this.addFieldError("password", "密码不能为空");
            return;
        }
        if (this.getUsername().length() < 5 || this.getUsername().length() > 16) {
            this.addFieldError("username", "用户名长度为5-16");
            return;
        }
    }
}
